package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.task.Cache;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.*;
import com.feicheng.persistence.mapper.AjiaolianMapper;
import com.feicheng.persistence.service.*;
import com.feicheng.service.a.FDzService;
import com.feicheng.service.a.FKcService;
import com.feicheng.wxapi.app.WxApi;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController("/wx/mdjl")
@RequestMapping("/wx/mdjl")
public class MdjlController {

    @Autowired
    FDzService dzService;

    @Autowired
    IAmountService iAmountService;

    @Autowired
    IClientService clientService;

    @Autowired
    IAdzService adzService;

    @Autowired
    IAzjService azjService;

    @Autowired
    IAzjgsService azjgsService;

    @Autowired
    IAstoreService astoreService;

    @Autowired
    IAcommissionService acommissionService;

    @Autowired
    IAmdjlService amdjlService;

    @Autowired
    IAmdjlsqitemService amdjlsqitemService;

    @Autowired
    FKcService kcService;

    @Autowired
    WxApi wxApi;

    @Autowired
    Cache cache;

    @Autowired
    HttpSession session;

    @PostMapping("/get")
    public ResultInfo getMdjl(@RequestBody JSONObject param) {

        String mdjlid = param.getString("mdjlid");

        Amdjl amdjl = amdjlService.getById(mdjlid);
        param.put("amdjl", amdjl);

        if (null == amdjl.getStoreid()) {
            Astore astore = astoreService.getById(amdjl.getStoreid());
            param.put("astore", astore);
        }

        if (null == amdjl.getDzid()) {
            Adz adz = adzService.getById(amdjl.getDzid());
            param.put("adz", adz);
        }

        QueryWrapper<Amdjlsqitem> qw = new QueryWrapper<>();
        qw.eq("mdjlid", mdjlid);
        param.put("sqs", amdjlsqitemService.list(qw));

        return ResultInfo.success(param);
    }

    @PostMapping("/delete")
    public ResultInfo jiebangMdjl(@RequestBody JSONObject param) {

        String mdjlid = param.getString("mdjlid");

        UpdateWrapper<Amdjl> uqw = new UpdateWrapper<Amdjl>();
        uqw.set("storeid", null);
        uqw.set("dzid", null);
        uqw.set("tx", null);
        uqw.set("tel", null);
        uqw.eq("mdjlid", mdjlid);
        amdjlService.update(uqw);

        return ResultInfo.success(param);
    }

    @Transactional
    @PostMapping("/phone")
    public ResultInfo changePhone(@RequestBody JSONObject param) {

        String mdjlid = param.getString("mdjlid");
        String phoneCode = param.getString("phoneCode");
        JSONObject result = new JSONObject();

        Asetting asetting = cache.getObject("Asetting", Asetting.class);

        String phone = wxApi.getPhoneNumber(
                "",
                asetting.getDappid(),
                asetting.getDsecretkey(),
                phoneCode);

        Amdjl amdjl = amdjlService.getById(mdjlid);
        Client client = clientService.getById(amdjl.getClientid());

        client.setTel(phone);
        amdjl.setTel(phone);

        clientService.updateById(client);
        amdjlService.updateById(amdjl);

        result.put("client", client);
        result.put("amdjl", amdjl);
        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, result);

        return ResultInfo.success(result);
    }

    @PostMapping("/info/put")
    public ResultInfo updateMdjl(@RequestBody JSONObject param) {
        Amdjl amdjl = param.getObject("amdjl", Amdjl.class);
        if(!StringUtils.hasLength(amdjl.getMdjlid())){
            throw new SystemException("门店教练id不能为空");
        }
        amdjlService.updateById(amdjl);
        param.put("amdjl", amdjl);
        return ResultInfo.success(param);
    }

    @PostMapping("/kaike")
    public ResultInfo kaike(@RequestBody JSONObject param) {
        return ResultInfo.success(kcService.mdjlkaike(param));
    }

    @PostMapping("/sq")
    public ResultInfo mdjlsq(@RequestBody JSONObject param) {
        Amdjlsqitem amdjlsqitem = param.getObject("amdjlsqitem", Amdjlsqitem.class);
        if(!StringUtils.hasLength(amdjlsqitem.getMdjlid())){
            throw new SystemException("门店教练id不能为空");
        }
        if(!StringUtils.hasLength(amdjlsqitem.getStoreid())){
            throw new SystemException("门店id不能为空");
        }
        amdjlsqitem.setMdjlsqid(StringGenerator.getID("MLSQ"));
        amdjlsqitem.setCt(LocalDateTime.now());

        amdjlsqitemService.save(amdjlsqitem);

        param.put("amdjlsqitem", amdjlsqitem);
        return ResultInfo.success(param);
    }

    @PostMapping("/sq/page")
    public ResultInfo getmdjlsqByPage(@RequestBody JSONObject param) {

        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");

        if (null == current || current < 0) {
            current = 1;
        }

        if (null == size || size < 0) {
            size = 16;
        }

        IPage<Amdjlsqitem> page = new Page<Amdjlsqitem>(current, size);
        QueryWrapper<Amdjlsqitem> qw = new QueryWrapper<>();

        String dzid = param.getString("dzid");
        if (StringUtils.hasLength(dzid)) {
            QueryWrapper<Astore> mdqw = new QueryWrapper<>();
            mdqw.eq("dzid", dzid);
            mdqw.select("storeid");
            List<Astore> mds = astoreService.list(mdqw);

            if (null != mds && mds.size() > 0) {
                JSONArray mdids = new JSONArray();
                mds.stream().forEach(md -> {
                    mdids.add(md.getStoreid());
                });
                qw.in("storeid", mdids);
            }
        }

        String mdjlid = param.getString("mdjlid");
        if (StringUtils.hasLength(mdjlid)) {
            qw.eq("mdjlid", mdjlid);
        }

        page = amdjlsqitemService.page(page, qw);
        param.put("sqs", page);

        return ResultInfo.success(param);
    }

    @PostMapping("/sq/list")
    public ResultInfo getmdjlsqByList(@RequestBody JSONObject param) {

        QueryWrapper<Amdjlsqitem> qw = new QueryWrapper<>();

        String dzid = param.getString("dzid");
        if (StringUtils.hasLength(dzid)) {
            QueryWrapper<Astore> mdqw = new QueryWrapper<>();
            mdqw.eq("dzid", dzid);
            mdqw.select("storeid");
            List<Astore> mds = astoreService.list(mdqw);

            if (null != mds && mds.size() > 0) {
                JSONArray mdids = new JSONArray();
                mds.stream().forEach(md -> {
                    mdids.add(md.getStoreid());
                });
                qw.in("storeid", mdids);
            }
        }

        String mdjlid = param.getString("mdjlid");
        if (StringUtils.hasLength(mdjlid)) {
            qw.eq("mdjlid", mdjlid);
        }

        param.put("sqs", amdjlsqitemService.list(qw));

        return ResultInfo.success(param);
    }

    @Transactional
    @PostMapping("/sq/delete")
    public ResultInfo setsq(@RequestBody JSONObject param) {
        String mdjlsqid = param.getString("mdjlsqid");
        if(!StringUtils.hasLength(mdjlsqid)){
            throw new SystemException("门店绑定申请id不能为空");
        }
        Amdjlsqitem amdjlsqitem = amdjlsqitemService.getById(mdjlsqid);

        String flg = param.getString("flg");

        if ("Y".equals(flg)) {
            Amdjl amdjl = amdjlService.getById(amdjlsqitem.getMdjlid());
            Astore astore = astoreService.getById(amdjlsqitem.getStoreid());
            amdjl.setStoreid(astore.getStoreid());
            amdjl.setDzid(astore.getDzid());
            amdjlService.updateById(amdjl);
        }

        amdjlsqitemService.removeById(mdjlsqid);
        param.put("amdjlsqitem", amdjlsqitem);
        return ResultInfo.success(param);
    }

}
